'''
Created on Feb 9, 2012

@author: xzhu
'''
import stateHead, const

class ioniState():
    '''
    An ionization state of residues ozain the pathway
    '''

    def __init__(self):
        '''
        Constructor
        '''
        
        self.crgs       = []          # charge on each residue
        self.ener       = 0.0         # energy of this state (boltzmann averaged)
        
        
        self.avg        = 0.0         # average energy of the state (regular average)
        self.std        = 0.0         # std of the energies of different microstates of this ionization state
        self.n_ms       = 0           # number of microstates
        
        
        self.seq        = 0
        self.ID         = ''
        self.count      = 0   # how many times it shows up in paths
        self.tier       = 0   # in which step this state is shown in transfer
        
    def init_from_state(self, state):
        '''
        copy constructor
        '''
        
        self.crgs  = state.crgs
        self.ener  = state.ener
        self.avg   = state.avg
        self.std   = state.std
        self.n_ms  = state.n_ms
        self.seq   = state.seq
        self.ID    = state.ID
        self.count = state.count
        self.tier  = state.tier
        
    def load_state(self, line):
        fields = line.split()
        for i in range(stateHead.stateHead.n_res):
            self.crgs.append(const.SYMBOLTOINT[fields[i]])
            self.ID += fields[i]
            
        self.ener   = float(fields[stateHead.stateHead.n_res])
        self.avg    = float(fields[stateHead.stateHead.n_res + 1])
        self.std    = float(fields[stateHead.stateHead.n_res + 2])
        self.n_ms   = float(fields[stateHead.stateHead.n_res + 3])
        
    def is_same(self, state):
        '''
        test if two states are the same
        '''
        return (self.crgs == state.crgs)
    
    
    def load_init_state(self, allstates, ifile):
        '''
        load the initial state, after loading all states
        '''
        crgs = []
        for field in open(ifile).readline().split():
            crgs.append(const.SYMBOLTOINT[field])
            
        ioniState.init_from_state(self, allstates.state_of_crg(crgs))
        
    def display(self):
        for crg in self.crgs:
            print const.INTTOSYMBOL[crg] + '\t',
        print self.ener,
        
    def __str__(self):
        return_str = ''
        for crg in self.crgs:
            return_str += (const.INTTOSYMBOL[crg] + '\t')
        return_str += str(self.ener)
            
        return return_str
